package cn.anecansaitin.whimsy.common.particle;

import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.BreakingItemParticle;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.particle.ParticleProvider;
import net.minecraft.core.particles.ItemParticleOption;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;

public class NoPhysicalItemParticle extends BreakingItemParticle {
    private final Vec3 startPos;
    private Vec3 endPos = Vec3.ZERO;

    protected NoPhysicalItemParticle(ClientLevel pLevel, double pX, double pY, double pZ, ItemStack pStack) {
        super(pLevel, pX, pY, pZ, pStack);
        startPos = new Vec3(pX, pY, pZ);
        hasPhysics = false;
    }
    //这个粒子效果构造函数中的速度被用来设置终点坐标了，意味着速度被定死为0.5
    NoPhysicalItemParticle(ClientLevel pLevel, double pX, double pY, double pZ, double pXSpeed, double pYSpeed, double pZSpeed, ItemStack pStack) {
        this(pLevel, pX, pY, pZ, pStack);
        endPos = new Vec3(pXSpeed, pYSpeed, pZSpeed);
        Vec3 direction = (endPos).subtract(new Vec3(pX, pY, pZ)).normalize().multiply(new Vec3(0.5, 0.5, 0.5));
        this.xd *= 0.1F;
        this.yd *= 0.1F;
        this.zd *= 0.1F;
        this.xd += direction.x;
        this.yd += direction.y;
        this.zd += direction.z;
    }

    @Override
    public void tick() {
        this.xo = this.x;
        this.yo = this.y;
        this.zo = this.z;

        if (this.age++ >= this.lifetime) {
            this.remove();
        } else {
            this.move(this.xd, this.yd, this.zd);

            if (endPos.subtract(startPos).length() < startPos.subtract(x, y, z).length()) {
                remove();
                return;
            }

            if (this.speedUpWhenYMotionIsBlocked && this.y == this.yo) {
                this.xd *= 1.1D;
                this.zd *= 1.1D;
            }

            this.xd *= this.friction;
            this.yd *= this.friction;
            this.zd *= this.friction;

            if (this.onGround) {
                this.xd *= 0.7F;
                this.zd *= 0.7F;
            }
        }
    }

    public static class Provider implements ParticleProvider<ItemParticleOption> {
        public Particle createParticle(ItemParticleOption pType, ClientLevel pLevel, double pX, double pY, double pZ, double pXSpeed, double pYSpeed, double pZSpeed) {
            return new NoPhysicalItemParticle(pLevel, pX, pY, pZ, pXSpeed, pYSpeed, pZSpeed, pType.getItem());
        }
    }
}
